package com.lh.pospi.x_store.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;

import com.lh.pospi.x_store.R;
import com.lh.pospi.x_store.ui.overflow.OverflowActivity;
import com.lh.pospi.x_store.ui.wholesale.WholesaleActivity;
import com.lh.pospi.x_store.utils.Constant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class BsFragment extends Fragment implements AdapterView.OnItemClickListener {
    @BindView(R.id.gridview)
    GridView gridview_main;
    private SimpleAdapter adapter;
    private List<Map<String, Object>> dataList;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.activity_main, null);
        ButterKnife.bind(this,v);
        String[] from={"img","text"};
        int[] to={R.id.img,R.id.text};
        initData();
        adapter=new SimpleAdapter(getActivity(), dataList, R.layout.grid_item, from, to);
        gridview_main.setAdapter(adapter);
        gridview_main.setOnItemClickListener(this);
        return v;
    }

    public static BsFragment getInstance() {
        BsFragment fragment = new BsFragment();
        return fragment;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent intent = new Intent(getActivity(), OverflowActivity.class);
        switch (dataList.get(i).get("text").toString()) {
            case "报损单":
                intent.putExtra(Constant.OVERFLOW, Constant.TYPE_BS);
                startActivity(intent);
                break;
            case "损溢单":
                intent.putExtra(Constant.OVERFLOW, Constant.TYPE_BY);
                startActivity(intent);
                break;


        }
    }


    private void initData() {
        //图标
        int icno[] = {R.mipmap.icon_cgdd,R.mipmap.icon_cgsh};
        //图标下的文字
        String name[]={"报损单","损溢单",
                };
        dataList = new ArrayList<Map<String, Object>>();
        for (int i = 0; i <icno.length; i++) {
            Map<String, Object> map=new HashMap<String, Object>();
            map.put("img", icno[i]);
            map.put("text",name[i]);
            dataList.add(map);
        }
    }
}
